<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_modal_EditAliasAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{    
        $aliasService = m_backoffice_services_AliasService::getInstance();
        $docTypes = $aliasService->getAuthorizedAliasDocTypes($this->getRequestParameter('configModule'));
        $lang = $this->getRequestParameter('docLang', $this->getContextLang());

        $formValues = $this->getFormValues('form');
        $alias = f_document_Provider::getInstance()->getByModelAndUniqueId('backoffice/Alias', $this->getRequestParameter('docId'), $this->getRequestParameter('docLang'));

        if(count($formValues) > 0)
        {
            $aliasedDoc = $this->getDocumentProvider()->getByUniqueId($formValues['target'], $this->getRequestParameter('docLang'));

            $alias  ->setAliasDocumentId($formValues['target'])
                    ->setAliasDocumentModel($aliasedDoc->getModel())
                    ->setAliasModule($aliasedDoc->getDocumentModuleName())
                    ->save(false);

            $this->closeModalAndRefreshMenu();
        }

        if(count($docTypes['enabled']) >0)
        {
            $list = m_backoffice_services_SelectService::getInstance()->setModelsToGet($docTypes['all'])
                                                                      ->disableModel($docTypes['disabled'])
                                                                      ->currentId($alias->getAliasDocumentId())
                                                                      ->setFolderModule($this->getRequestParameter('configModule'))
                                                                      ->execute();

            $this->addToModel('hasValues', $list->getSelectableItemsNb() > 0);
            $this->addToModel('list', $list->getSelectContent());
        }

        $this->addToModel('selectedId', $alias->getAliasDocumentId());
        $this->setModalParameters();
		$this->setView('success');
	}
}